package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Boke;
import model.BokeDao;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/author")
public class AuthorServlet extends HttpServlet {

    private ObjectMapper objectMapper=new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bokeId=req.getParameter("bokeId");

        if(bokeId==null)
        {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("参数非法，缺少bokeId");
            return;
        }

        BokeDao bokeDao=new BokeDao();
        Boke boke=bokeDao.select(Integer.parseInt(bokeId));
        if(boke==null)
        {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("没找到指定的博客"+bokeId);
            return;
        }

        UserDao userDao=new UserDao();
        User author=userDao.selectById(boke.getUserId());
        String respJson=objectMapper.writeValueAsString(author);
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(respJson);

    }
}
